package cn.kgc.controller;

import cn.kgc.base.dto.UmsRoleDTO;
import cn.kgc.client.UmsRoleFeignClient;
import cn.kgc.commons.annoation.AutoDict;
import cn.kgc.commons.exception.HttpException;
import cn.kgc.commons.util.UserContextHolder;
import cn.kgc.commons.vo.Result;
import cn.kgc.dto.add.WmsPurchaseAddDTO;
import cn.kgc.entity.WmsPurchase;
import cn.kgc.service.WmsPurchaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>采购单控制器</p>
 *
 * @author 卢传琪
 */
@RestController
@Api(tags = "采购单控制器")
@RequestMapping("wms/purchase")
@Validated
@Slf4j
public class WmsPurchaseController {

    @Resource
    private WmsPurchaseService wmsPurchaseService;

    @Resource
    private UmsRoleFeignClient umsRoleFeignClient;

    //采购人员接收采购任务
    @PostMapping("/accept")
    @ApiOperation(value = "采购人员接收采购单", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    @ApiImplicitParam(name = "id", value = "采购单id", paramType = "query", dataType = "long")
    public boolean acceptWmsPurchase(@RequestParam(name = "id") Long id) {
        return this.wmsPurchaseService.acceptWmsPurchase(id);
    }

    @PostMapping("/save")
    @CrossOrigin
    @ApiOperation(value = "新建采购单",httpMethod = "POST",
            produces = "application/json;charset=utf-8",response = Result.class)
    public boolean saveWmsPurchase(@RequestBody WmsPurchaseAddDTO wmsPurchaseAddDTO){
        return wmsPurchaseService.saveWmsPurchase(wmsPurchaseAddDTO);
    }

    @PostMapping("/finish")
    @CrossOrigin
    @ApiOperation(value = "采购人员完成采购任务",httpMethod = "POST",
            produces = "application/json;charset=utf-8",response = Result.class)
    public boolean finishWmsPurchase(@RequestParam(name = "id") Long id){
        return wmsPurchaseService.finishWmsPurchase(id);
    }

    //查看采购单信息,普通采购人员可以查看自己的采购单，采购的管理人员，查看所有的采购单
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数,默认5条", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "wmsPurchaseId", value = "采购单id", paramType = "query", dataType = "long")
    })
    @CrossOrigin
    @AutoDict
    public IPage<WmsPurchase> selectAll(@RequestParam(defaultValue = "1") int current,
                                        @RequestParam(defaultValue = "5") int pageSize,
                                        @RequestParam(defaultValue = "0") long wmsPurchaseId
    ){
        //long memberId=Long.parseLong(UserContextHolder.getInstance().getUserId());
        long memberId=1L;
        //根据userId获得用户角色,根据角色判断类型
        List<UmsRoleDTO> list=umsRoleFeignClient.getUmsRole(memberId).getData();
        if (list==null){
            throw new HttpException(40002,500);
        }
        boolean flag=false;
        for (UmsRoleDTO umsRoleDTO : list) {
            if (umsRoleDTO.getRoleCode().equals("CGGLY")){
                flag=true;
                break;
            }
        }
        LambdaQueryWrapper<WmsPurchase> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (!flag){
            lambdaQueryWrapper.eq(WmsPurchase::getAssigneeId,memberId);
        }
        lambdaQueryWrapper.eq(wmsPurchaseId!=0,WmsPurchase::getId,wmsPurchaseId);
        IPage<WmsPurchase> page = new Page(current, pageSize);
        return wmsPurchaseService.page(page,lambdaQueryWrapper);
    }

   //采购人员接受采购任务

    //采购管理员将采购单分配给采购人员
    @PostMapping("/allocation")
    @CrossOrigin
    @ApiOperation(value = "分配采购单",httpMethod = "POST",
            produces = "application/json;charset=utf-8",response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assigneeId", value = "采购人id", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "assigneeName", value = "采购人姓名",paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "采购人电话", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "采购单id",paramType = "query", dataType = "Long")
    })
    public boolean updateWmsPurchase(@RequestParam("assigneeId") long assigneeId,
                                     @RequestParam("assigneeName")String assigneeName,
                                     @RequestParam("phone")String phone,
                                     @RequestParam("id")Long id){
        return wmsPurchaseService.allocationWmsPurchase(assigneeId,assigneeName,phone,id);
    }
}
